//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight
{
    using Core;
    using Model;

    public static class Engine
    {
        private static readonly object initializeLock = new object();
        private static IPreflightEngine engineInstance;

        public static IPreflightEngine Instance
        {
            get
            {
                if (!IsInitialized())
                {
                    if (ContextFromConfiguration.EngineConfigurationIsDefined)
                    {
                        Log.LogInformation("Attempting to initialize from configuration");
                        InitializeFromConfiguration();
                        Log.LogInformation("Initialization completed.");
                    }
                    else
                    {
                        Log.LogError("Attempted to access instance of engine but the engine has not been initialized");
                        throw new EngineNotInitializedException();
                    }
                }

                return engineInstance;
            }
        }

        public static void InitializeFromConfiguration()
        {
            var configurationReader = new ContextFromConfiguration();
            var context = configurationReader.Read();
            Initialize(context);
        }

        public static bool IsInitialized()
        {
            return engineInstance != null;
        }

        public static void Initialize(PreflightContext context)
        {
            lock (initializeLock)
            {
                if (IsInitialized())
                {
                    return;
                }

                var factory = new PreflightEngineFactory();
                engineInstance = factory.CreateEngine(context);
            }
        }

        public static void Reset()
        {
            engineInstance = null;
        }
    }
}
